import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';

export function IsImageFile(validationOptions?: ValidationOptions) {
  return function (object: any, propertyName: string) {
    registerDecorator({
      name: 'isImageFile',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: {
        validate(value: any, args: ValidationArguments) {
          if (!value) return false;  // 先检查 value 是否存在
          const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp'];
          return allowedTypes.includes(value.mimetype);
        },
        defaultMessage(args: ValidationArguments) {
          return '文件必须是图片（JPEG/JPG, PNG, GIF, BMP）';
        }
      }
    });
  };
}
